public class BinarySearchTree {

    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode root = null; //根节点

    /**
     * @param val
     * @description 查找二叉树搜索树中指定的val值
     */
    public TreeNode find(int val){
        TreeNode cur  = root;
       while(cur!=null) {
           if (cur.val == val) {
               return cur;
           } else if (cur.val < val) {
               cur = cur.right;
           } else {
               cur = cur.left;
           }
       }
       return null;
    }
    /**
     * @param val
     * @description 插入一个数据
     */
    public void insert(int val){
        if(root == null){
            root = new TreeNode(val);
            return;
        }
        TreeNode cur  = root;
        TreeNode prev = null;
        while(cur!=null){
            if(cur.val < val){
                prev = cur;
                cur = cur.right;
            }else if(cur.val > val){
                prev = cur;
                cur = cur.left;
            }else {
                return;
            }
        }
        if(prev.val >val){
            prev.left = new TreeNode(val);
        }else if(prev.val < val){
            prev.right = new TreeNode(val);
        }
    }
    public void inorder(TreeNode root){
        if(root == null){
            return;
        }
        inorder(root.left);
        System.out.print(root.val + " ");
        inorder(root.right);
    }

    /**
     * @param val
     * @description 删除值为val的节点
     */
    public void remove(int val){
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur!=null){
           if(cur.val == val){
                removeNode(parent,cur);
                return;
           }else if(cur.val < val){
               parent = cur;
               cur = cur.right;
           }else {
                parent = cur;
                cur = cur.left;
           }
        }
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null){
            if(cur == parent){
                root= cur.right;
            }else if(cur == parent.left){
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null){
            if(cur == root){
                root = cur.left;
            }else if(cur == parent.left){
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {//cur.left != null && cur.right != null
            TreeNode target = cur.right;
            TreeNode targetP = cur;
            while(target.left != null){//一直往左走，找到最后的节点，就是右树的最小值
                targetP = target;
                target = target.left;
            }
            cur.val = target.val; //替换
            if(target == targetP.left){
                targetP.left = target.right;
            }else { //如若左子树是空呢？
                targetP.right = target.right;
            }

        }
    }


}
